package sctu.edu.java.year2016.team06.luoxin.day20160607.chapter15_3;

import sun.awt.SunToolkit;

import javax.swing.*;
import java.awt.*;

/**尝试使开发一个窗体，在窗体中设计一个进度条，使进度条每次递增滚动。
 * Created by Administrator on 2016/6/7.
 */
public class ProgressBar  extends JFrame {
    Thread t1=new Thread();
    Thread t2= new Thread();
    private  int  count;

    public static void main(String[] args) {
     // init( new ProgressBar(),150,100) ;
    }
    public ProgressBar(){
        deValue();
        addValue();
        t1.start();
        t2.start();
    }
    public void addValue(){
        t1=new Thread(new Runnable() {                                 //实例化线程t1
            @Override
            public void run() {
                while(true){
                    if(count>=100){
                        System.out.println("进度条已满，递增线程等待");
                        break;
                    }
                    if(count==0){
                        //.setValue(count += 100);
                        System.out.println("进度条的当前值为："+count);
                        synchronized (t2){                              //定义同步块
                            System.out.println("进度条已有值，可以进行递减操作");
                            t2.notify();                               //在同步中将线程t2唤醒
                        }
                    }

                        try {
                            Thread.currentThread().sleep(100);      //使当前线程休眠100毫秒
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }



                }
            }
        });

    }
    public void deValue(){
        t2=new Thread(new Runnable() {                          //定义线程t2
            @Override
            public void run() {
                while(true){
                    if(count==0) {
                        synchronized (this) {                 //定义同步块
                        try {
                            wait();                          //使线程t2等待
                        }catch(Exception e){
                            e.printStackTrace();
                        }
                        }
                    }
                //    progressBar.setValue(--count);        //将进度条的当前值递减
                    System.out.println("进度条的当前值为："+count);
                    try{
                                Thread.currentThread().sleep(100);
                    }
                    catch(Exception e){
                        e.printStackTrace();
                    }
                }

            }
        });
    }
}
